$(document).ready(function(){

    $("#message").hide();
    $("#error-identity").hide();
    $("#error-pass").hide();
    $("#error-re-pass").hide();
    $("#error-name-repeat").hide();
    $("#error-name").hide();
    $("#error-email").hide();

    /*隐藏完成完成注册表单*/
    $("#okrgt").hide();
    $("#username").hide();
    $("#email").hide();

    var reg_identity = /^[a-zA-Z0-9]{5,10}$/;
    var reg_pass = /^[a-zA-Z0-9]{6,16}$/;
    var reg_email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    var reg_name = /^[a-zA-Z0-9\u4e00-\u9fa5]{2,12}$/;

    /**
     * 全局变量
     */
    var public_identity,public_password,public_re_password,public_email,public_username;

    $("#rgt").click(function(){
        var identity = $("#identity").val();
        var password = $("#password").val();
        var re_password = $("#re-password").val();

        public_identity = identity;
        public_password = password;
        public_re_password = re_password;

        //校验用户注册时的信息是否合法
        if(!reg_identity.test(identity)){
            $("#error-identity").show(500);
            $("#error-pass").hide();
            $("#error-pass").hide();
            $("#message").hied();
        }else{
            $("#error-identity").hide(500);
            if(!reg_pass.test(password)){
                $("#error-pass").show(500);
                $("#error-identity").hide();
                $("#message").hide();
                $("#error-re-pass").hide(500);
            }else{
                $("#error-pass").hide(500);
                if(re_password!=password){
                    $("#error-re-pass").show(500);
                    $("#error-pass").hide();
                    $("#error-identity").hide();
                }else{
                    $("#error-re-pass").hide();
                    //显示完成注册表单
                    /*隐藏校验注册信息按钮*/
                    $("#rgt").hide(500);
                    /*隐藏已有账号*/
                    $("#mes-have").hide(500);
                    /*隐藏确认密码*/
                    $("#div-re-password").hide(500);
                    /*隐藏密码*/
                    $("#div-password").hide(500);
                    /*隐藏账号*/
                    $("#div-identity").hide(500);
                    /*显示邮箱,用户名输入框和完成注册输入框*/
                    $("#username").show(1000);
                    $("#email").show(1000);
                    $("#mes-have").show(1000);
                    $("#okrgt").show(1000);

                    /*
                     //封装用户注册信息
                     var register_data = {
                         "identity":username,
                         "password":password
                     }
                     //发送ajax请求
                     $("#message").show(500);*/
                }
            }
        }

    });

    $("#okrgt").click(function(){
        if(public_identity==null||public_password==null||public_re_password==null){
            window.location.href = "../exception/register_error.jsp";
        }

        var email = $("#email").val();
        var username = $("#username").val();

        public_email = email;
        public_username = username;

        /*判断用户名是否合法*/
        if(!reg_name.test(username)){
            $("#error-name").show(500);
            $("#error-email").hide();
        }else{
            /*判断邮箱是否合法*/
            if(!reg_email.test(email)){
                $("#error-email").show(500);
                $("#error-name").hide();
            }else{
                $("#error-email").hide();
                $("#error-name").hide();
                console.log(public_identity);
                console.log(public_password);
                console.log(public_re_password);
                console.log(public_username);
                console.log(public_email);
                /*封装用户注册信息*/
                var data = {
                    "identity":public_identity,
                    "password":public_password,
                    "re_password":public_re_password,
                    "username":public_username,
                    "email":public_email
                }
                request_ajax_register(data);
            }
        }
    });

   function request_ajax_register(data){
        $.ajax({
            url:"/register/addnewuser.keyboard",
            type:"post",
            data:data,
            success:function(result){
                if(result.code == 100){
                    $("#message").show(500);
                }else{
                    alert(result.msg);
                }
            }
        })
   }

});